package org.jeecg.modules.wangyue.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.entity.PerformanceFlow;
import org.jeecg.modules.wangyue.service.IPerformanceFlowService;
import org.jeecg.modules.wangyue.utils.ExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
* @Description: 业绩流水管理
* @Author: jeecg-boot
* @Date:   2023-06-30
* @Version: V1.0
*/
@Slf4j
@Api(tags="业绩流水管理")
@RestController
@RequestMapping("/wangyue/sys/performanceFlow")
public class PerformanceFlowManageController extends JeecgController<PerformanceFlow, IPerformanceFlowService> {
   @Autowired
   private IPerformanceFlowService performanceFlowService;
   @Autowired
   private ExportUtil exportUtil;


   /**
    * 业绩流水管理-分页列表查询
    *
    * @param keywords		关键字
    * @param userType		用户类型
    * @param changeType	    变动类型(1-注册业绩，2-推广邀请业绩)
    * @param beginTime		开始时间
    * @param endTime		结束时间
    * @param pageNo		    第几页
    * @param pageSize		每页大小
    * @return
    */
   @AutoLog(value = "业绩流水管理-分页列表查询")
   @ApiOperation(value="业绩流水管理-分页列表查询", notes="业绩流水管理-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(@RequestParam(name="keywords", required=false) String keywords,
                                  @RequestParam(name="userType", required=false) String userType,
                                  @RequestParam(name="changeType", required=false) String changeType,
                                  @RequestParam(name="beginTime", required = false) String beginTime,
                                  @RequestParam(name="endTime", required = false) String endTime,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
       QueryWrapper<PerformanceFlow> queryWrapper = new QueryWrapper<>();
       if(oConvertUtils.isNotEmpty(keywords)){
           queryWrapper.like("account", keywords);
       }
       if(oConvertUtils.isNotEmpty(userType)){
           queryWrapper.eq("user_type", userType);
       }
       if(oConvertUtils.isNotEmpty(changeType)){
           queryWrapper.eq("change_type", changeType);
       }
       if(oConvertUtils.isNotEmpty(beginTime) && oConvertUtils.isNotEmpty(endTime)){
           queryWrapper.apply("create_time >= '" + beginTime + "' and create_time <= '" + endTime + "'");
       }
       queryWrapper.eq("del_flag", DelFlagConstant.normal);
       queryWrapper.orderByDesc("create_time");
       Page<PerformanceFlow> page = new Page<PerformanceFlow>(pageNo, pageSize);
       IPage<PerformanceFlow> pageList = performanceFlowService.page(page, queryWrapper);
       return Result.OK(pageList);
   }



    /**
     * 导出excel
     *
     * @param performanceFlow
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(PerformanceFlow performanceFlow) {
        QueryWrapper<PerformanceFlow> queryWrapper = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(performanceFlow.getIds())){
            queryWrapper.in("id", Arrays.asList(performanceFlow.getIds().split(",")));
        }
        if(oConvertUtils.isNotEmpty(performanceFlow.getKeywords())){
            queryWrapper.like("account", performanceFlow.getKeywords());
        }
        if(oConvertUtils.isNotEmpty(performanceFlow.getUserType())){
            queryWrapper.eq("user_type", performanceFlow.getUserType());
        }
        if(oConvertUtils.isNotEmpty(performanceFlow.getChangeType())){
            queryWrapper.eq("change_type", performanceFlow.getChangeType());
        }
        if(oConvertUtils.isNotEmpty(performanceFlow.getBeginTime()) && oConvertUtils.isNotEmpty(performanceFlow.getEndTime())){
            queryWrapper.apply("create_time >= '" + performanceFlow.getBeginTime() + "' and create_time <= '" + performanceFlow.getEndTime() + "'");
        }
        queryWrapper.eq("del_flag", DelFlagConstant.normal);
        queryWrapper.orderByDesc("create_time");
        List<PerformanceFlow> performanceFlowList = performanceFlowService.list(queryWrapper);
        return exportUtil.exportXls(performanceFlowList, PerformanceFlow.class, "业绩流水数据");
    }


}
